<?php

namespace App\Admin\Controllers;

use App\Models\WorkWechatUser;
use App\Models\WxWorkChatData;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class WxWorkChatDataController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '企业微信会话内容';

    protected function list(Content $content)
    {
        // 选填
        $content->title($this->title);

        // 选填
        $content->description('列表');

        // 添加面包屑导航 since v1.5.7
        $content->breadcrumb(
            ['text' => '首页', 'url' => '/admin'],
            ['text' => '企业微信', 'url' => '/admin/wx_work_chat_data'],
            ['text' => '会话内容']
        );

        $wxWorkChatData = WxWorkChatData::query()->with(['workWechatUser'])->orderByDesc('seq')->paginate();
//        $wxWorkChatData = WxWorkChatData::query()->with(['workWechatUser'])->orderByDesc('seq')->first();
//        dd($wxWorkChatData);

        // 直接渲染视图输出，Since v1.6.12
        $content->view('admin.wx_work_chat_data.list', ['list' => $wxWorkChatData]);

        return $content;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WxWorkChatData());

        $grid->column('id', __('Id'));
        $grid->column('seq', __('Seq'));
        $grid->column('msgid', __('Msgid'));
        $grid->column('msg', __('Msg'))->style('width:50px');
        $grid->column('data', __('Data'))->style('width:50px');
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->disableCreateButton();

        $grid->actions(function ($actions) {
            // 去掉编辑
            $actions->disableEdit();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WxWorkChatData::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('seq', __('Seq'));
        $show->field('msgid', __('Msgid'));
        $show->field('msg', __('Msg'))->json();
        $show->field('data', __('Data'))->json();
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WxWorkChatData());

        $form->number('seq', __('Seq'));
        $form->text('msgid', __('Msgid'));
        $form->textarea('msg', __('Msg'));
        $form->textarea('data', __('Data'));

        return $form;
    }
}
